package com.captain.special.栈队列;

import java.util.Deque;
import java.util.LinkedList;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/6 22:53
 */
public class 有效的括号20 {
    public boolean isValid(String s) {
        //栈
        LinkedList<Character> deque = new LinkedList<>();
        char ch;
        for (int i = 0; i < s.length(); i++) {
            ch = s.charAt(i);
            if (ch == '(') {
                deque.push(')');
            } else if (ch == '{') {
                deque.push('}');
            } else if (ch == '[') {
                deque.push(']');
                // 右边
            }else if (deque.isEmpty() || ch != deque.peek()) {
                return false;
            }else {
                deque.pop();
            }
        }
        return deque.isEmpty();
    }
}
